package org.dromara.hm.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.hm.domain.bo.HmTodoTransferHistoryBo;
import org.dromara.hm.domain.vo.HmTodoTransferHistoryVo;
import org.dromara.hm.domain.HmTodoTransferHistory;
import org.dromara.hm.mapper.HmTodoTransferHistoryMapper;
import org.dromara.hm.service.IHmTodoTransferHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 待办事项归属转移历史Service业务层处理
 *
 * @author Hzy
 * @date 2025-06-13
 */
@RequiredArgsConstructor
@Service
public class HmTodoTransferHistoryServiceImpl implements IHmTodoTransferHistoryService {

    private final HmTodoTransferHistoryMapper baseMapper;

    /**
     * 查询待办事项归属转移历史
     *
     * @param todoTransferHistoryId 主键
     * @return 待办事项归属转移历史
     */
    @Override
    public HmTodoTransferHistoryVo queryById(Long todoTransferHistoryId){
        return baseMapper.selectVoById(todoTransferHistoryId);
    }

    /**
     * 分页查询待办事项归属转移历史列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 待办事项归属转移历史分页列表
     */
    @Override
    public TableDataInfo<HmTodoTransferHistoryVo> queryPageList(HmTodoTransferHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HmTodoTransferHistory> lqw = buildQueryWrapper(bo);
        Page<HmTodoTransferHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的待办事项归属转移历史列表
     *
     * @param bo 查询条件
     * @return 待办事项归属转移历史列表
     */
    @Override
    public List<HmTodoTransferHistoryVo> queryList(HmTodoTransferHistoryBo bo) {
        LambdaQueryWrapper<HmTodoTransferHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HmTodoTransferHistory> buildQueryWrapper(HmTodoTransferHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmTodoTransferHistory> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HmTodoTransferHistory::getTodoTransferHistoryId);
        lqw.eq(bo.getCustomerTransferHistoryId() != null, HmTodoTransferHistory::getCustomerTransferHistoryId, bo.getCustomerTransferHistoryId());
        lqw.eq(bo.getTodoId() != null, HmTodoTransferHistory::getTodoId, bo.getTodoId());
        lqw.eq(bo.getCustomerId() != null, HmTodoTransferHistory::getCustomerId, bo.getCustomerId());
        lqw.eq(bo.getOldTodoPersonId() != null, HmTodoTransferHistory::getOldTodoPersonId, bo.getOldTodoPersonId());
        lqw.like(StringUtils.isNotBlank(bo.getOldTodoPersonName()), HmTodoTransferHistory::getOldTodoPersonName, bo.getOldTodoPersonName());
        lqw.eq(bo.getNewTodoPersonId() != null, HmTodoTransferHistory::getNewTodoPersonId, bo.getNewTodoPersonId());
        lqw.like(StringUtils.isNotBlank(bo.getNewTodoPersonName()), HmTodoTransferHistory::getNewTodoPersonName, bo.getNewTodoPersonName());
        lqw.eq(bo.getOperatorId() != null, HmTodoTransferHistory::getOperatorId, bo.getOperatorId());
        lqw.like(StringUtils.isNotBlank(bo.getOperatorName()), HmTodoTransferHistory::getOperatorName, bo.getOperatorName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), HmTodoTransferHistory::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增待办事项归属转移历史
     *
     * @param bo 待办事项归属转移历史
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HmTodoTransferHistoryBo bo) {
        HmTodoTransferHistory add = MapstructUtils.convert(bo, HmTodoTransferHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTodoTransferHistoryId(add.getTodoTransferHistoryId());
        }
        return flag;
    }

    /**
     * 修改待办事项归属转移历史
     *
     * @param bo 待办事项归属转移历史
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HmTodoTransferHistoryBo bo) {
        HmTodoTransferHistory update = MapstructUtils.convert(bo, HmTodoTransferHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HmTodoTransferHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除待办事项归属转移历史信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public void batchInsertHistories(List<HmTodoTransferHistory> todoHistoryList)
    {
        baseMapper.insertBatch(todoHistoryList);
    }
}
